##### SUBGROUP PLOTS #####
library(tidyverse)
library(broom)
library(estimatr)
library(ggpubr)

# function to extract ATEs
Get_ATE <- function(data, subsample = c(0,1)) {
      out <- tidy(lm_robust(formula(data$FORMULA[1]), data, 
                            subset = TNRFU %in% subsample))
      out %>% slice_tail() %>%
            mutate(StudyID = data$StudyId[1])
}

# set working directory
setwd("data/analysis/cleaned/")

#### STUDY 1 ####
d <- read_csv("TESS_001_SHANNON_CLEANED.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p1 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, linewidth = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, linewidth = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("1 Shannon") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S1_Shannon.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")


#### STUDY 2 ####
d <- read_csv("TESS_002_POWELL_CLEANED.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p2 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("2 Powell") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S2_Powell.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")


#### STUDY 3 ####
d <- read_csv("TESS_003_WILLIAMSON_CLEANED.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p3 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      ggtitle("3 Williamson") +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S3_Williamson.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")


#### STUDY 4 ####
d <- read_csv("TESS_004_TAK_CLEANED.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p4 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("4 Tak") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S4_Tak.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")


#### STUDY 5 ####

d <- read_csv("TESS_005_FARROW_CLEANED.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p5 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("5 Farrow") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S5_Farrow.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")




#### STUDY 6 ####
d <- read_csv("TESS_006_WALLACE_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p6 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("6 Wallace") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S6_Wallace.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")

#### STUDY 7 ####
d <- read_csv("TESS_007_HAALAND_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p7 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("7 Haaland") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S7_Haaland.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")


#### STUDY 8 ####
d <- read_csv("TESS_008_MUTZ_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ## [no nonwhites in this dataset]

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p8 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("8 Mutz") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))
#ggsave("S8_Mutz.pdf", height = 4, width = 4,
#path = "~/Dropbox/TESS Generalizability/Meta Analysis/Plots/ATE Plots/Study-Specific/")

#### STUDY 9 ####
d <- read_csv("TESS_009_BAUM_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p9 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("9 Baum") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 11 ####
d <- read_csv("TESS_011_BOUGHER_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ### [no inds in this study]
### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p11 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("11 Bougher") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 12 ####
d <- read_csv("TESS_012_SIMAS_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)
### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p12 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("12 Simas") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 13 ####
d <- read_csv("TESS_013_AHLER_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ### [no inds here]

all_results <- add_row(all_results, study_results)
### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p13 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("13 Ahler") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 14 ####
d <- read_csv("TESS_014_SCHNABEL_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p14 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("14 Schnabel") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 15 ####
d <- read_csv("TESS_015_CHENG_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p15 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("15 Cheng") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 16 ####
d <- read_csv("TESS_016_DIETZE_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p16 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("16 Dietze") + 
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 18 ####
d <- read_csv("TESS_018_MCCABE_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p18 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("18 McCabe") + 
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 19 ####
d <- read_csv("TESS_019_Ryan_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p19 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("19 Ryan") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 20 ####
d <- read_csv("TESS_020_BANDARA_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p20 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("20 Bandara") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 21 ####
d <- read_csv("TESS_021_CHU_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p21 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("21 Chu") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 22 ####
d <- read_csv("TESS_022_MIRELES_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p22 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("22 Mireles") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 23 ####
d <- read_csv("TESS_023_KENNEDY_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p23 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("23 Kennedy") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 24 ####
d <- read_csv("TESS_014_SCHNABEL_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p24 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("24 Hankinson") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 25 ####
d <- read_csv("TESS_025_TERMAN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p25 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("25 Terman") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 27 ####
d <- read_csv("TESS_027_HARBRIDGE-YONG_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p27 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("27 Harbridge-Yong") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 28 ####
d <- read_csv("TESS_028_SHANNON_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p28 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("28 Shannon") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 29 ####
d <- read_csv("TESS_029_HOWAT_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p29 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("29 Howat") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 30 ####
d <- read_csv("TESS_030_MORGAN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p30 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("30 Morgan") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 31 ####
d <- read_csv("TESS_031_SILVERMAN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p31 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("31 Silverman") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 32 ####
d <- read_csv("TESS_032_YADON_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ## [no whites in this one]
## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p32 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("32 Yadon") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 33 ####
d <- read_csv("TESS_033_HAMILTON_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p33 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("33 Hamilton") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 34 ####
d <- read_csv("TESS_034_BROWER_CLEAN.csv")
### GENDER ###

# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)
### MEN ### [no men in this one]
### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ### [none]

### Some college ### [none]


all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ## [none here]

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p34 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("34 Brower") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 35 ####
d <- read_csv("TESS_035_KRUPNIKOV_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p35 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("35 Krupnikov") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 36 ####
d <- read_csv("TESS_036_CALARCO_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p36 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("36 Calarco") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 37 ####
d <- read_csv("TESS_037_RIFKIN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p37 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("37 Rifkin") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 39 ####
d <- read_csv("TESS_039_HANKINSON_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p39 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("39 Hankinson") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 40 ####
d <- read_csv("TESS_040_THORSON_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p40 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("40 Thorson") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 41 ####
d <- read_csv("TESS_041_MELIN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p41 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("41 Melin") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 42 ####
d <- read_csv("TESS_042_VOGLER_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p42 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("42 Vogler") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 43 ####
d <- read_csv("TESS_043_KLAR_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p43 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("43 Klar") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 44 ####
d <- read_csv("TESS_044_COHEN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p44 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("44 Cohen") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 45 ####
d <- read_csv("TESS_045_BLAIR_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p45 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("45 Blair") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 46 ####
d <- read_csv("TESS_046_MARGOLIS_CLEANED.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p46 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("46 Margolis") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 47 ####
d <- read_csv("TESS_047_JAKUBIAK_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p47 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("47 Jakubiak") + 
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 48 ####
d <- read_csv("TESS_048_GRACE_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p48 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("48 Grace") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 50 ####
d <- read_csv("TESS_050_HEADLEY_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p50 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("50 Headley") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 51 ####
d <- read_csv("TESS_051_ZHU_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p51 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("51 Zhu") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 52 ####
d <- read_csv("TESS_052_HOLLIN_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p52 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("52 Hollin") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 53 ####
d <- read_csv("TESS_053_STOKER_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p53 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("53 Stoker") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### STUDY 54 ####
d <- read_csv("TESS_054_WEISSHAAR_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p54 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("54 Weisshaar") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))

#### STUDY 56 ####
d <- read_csv("TESS_056_BAI_CLEAN.csv")
### GENDER ###
### MEN ###


# start by making an empty data set 
all_results <- tibble(term = NA, estimate = NA, std.error = NA, statistic = NA,
                      p.value = NA, conf.low = NA, conf.high = NA, df = NA, 
                      outcome = NA, StudyID = NA, subsample = NA, subgroup = NA)

dMen <- filter(d, GENDER == 1)
study_results <- bind_rows(Get_ATE(dMen),
                           Get_ATE(dMen, 0), # eager respondents
                           Get_ATE(dMen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Men")

all_results <- add_row(all_results, study_results)

### WOMEN ###
# start by making an empty data set 
dWomen <- filter(d, GENDER == 2)
study_results <- bind_rows(Get_ATE(dWomen),
                           Get_ATE(dWomen, 0), # eager respondents
                           Get_ATE(dWomen, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Women")

all_results <- add_row(all_results, study_results)


### AGE (3 cat.) ###
## 18- to 39-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge1 <- filter(d, AGE3 == "18-39")

study_results <- bind_rows(Get_ATE(dAge1),
                           Get_ATE(dAge1, 0), # eager respondents
                           Get_ATE(dAge1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age1")

all_results <- add_row(all_results, study_results)

## 40- to 59-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))
dAge2 <- filter(d, AGE3 == "40-59")

study_results <- bind_rows(Get_ATE(dAge2),
                           Get_ATE(dAge2, 0), # eager respondents
                           Get_ATE(dAge2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age2")

all_results <- add_row(all_results, study_results)

## 60+-year-olds ##
d <- mutate(d, AGE3 = case_when(
      between(AGE, 18, 39) ~ "18-39",
      between(AGE, 40, 59) ~ "40-59",
      between(AGE, 60, 150) ~ "60+"
))

dAge3 <- filter(d, AGE3 == "60+")

study_results <- bind_rows(Get_ATE(dAge3),
                           Get_ATE(dAge3, 0), # eager respondents
                           Get_ATE(dAge3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Age3")

all_results <- add_row(all_results, study_results)

### Party ID (3 cat.) ###
### Democrats ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dDem <- filter(d, pid3 == "Democrat")

study_results <- bind_rows(Get_ATE(dDem),
                           Get_ATE(dDem, 0), # eager respondents
                           Get_ATE(dDem, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Democrats")

all_results <- add_row(all_results, study_results)
### Independents ###

dInd <- filter(d, pid3 == "Independent")
study_results <- bind_rows(Get_ATE(dInd),
                           Get_ATE(dInd, 0), # eager respondents
                           Get_ATE(dInd, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Independents")

all_results <- add_row(all_results, study_results)

### Republicans ###
d <- mutate(d, pid3 = case_when(
      between(PartyID7, 1, 3) ~ "Democrat",
      between(PartyID7, 4, 4) ~ "Independent",
      between(PartyID7, 5, 7) ~ "Republican",
      TRUE ~ NA_character_))
dRep <- filter(d, pid3 == "Republican")

study_results <- bind_rows(Get_ATE(dRep),
                           Get_ATE(dRep, 0), # eager respondents
                           Get_ATE(dRep, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Republicans")

all_results <- add_row(all_results, study_results)


### EDUCATION (3 cat.) ###

### HS Degree or less ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))
dEduc1 <- filter(d, EDUC4 == "1No HS degree" | EDUC4 == "2HS degree")

study_results <- bind_rows(Get_ATE(dEduc1),
                           Get_ATE(dEduc1, 0), # eager respondents
                           Get_ATE(dEduc1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "HS degree")

all_results <- add_row(all_results, study_results)
### Some college ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc2 <- filter(d, EDUC4 == "3Some college")

study_results <- bind_rows(Get_ATE(dEduc2),
                           Get_ATE(dEduc2, 0), # eager respondents
                           Get_ATE(dEduc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Some college")

all_results <- add_row(all_results, study_results)
### BA or higher ###
d <- mutate(d, EDUC4 = case_when(
      EDUC < 9 ~ "1No HS degree",
      EDUC == 9 ~ "2HS degree", 
      between(EDUC, 10, 11) ~ "3Some college",
      EDUC > 11 ~ "4BA or higher"))

dEduc3 <- filter(d, EDUC4 == "4BA or higher")

study_results <- bind_rows(Get_ATE(dEduc3),
                           Get_ATE(dEduc3, 0), # eager respondents
                           Get_ATE(dEduc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "BA or higher")

all_results <- add_row(all_results, study_results)

### INCOME (3 cat.) ###

## Lower Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc1 <- filter(d, INCOME3 == "Low Income")

study_results <- bind_rows(Get_ATE(d),
                           Get_ATE(d, 0), # eager respondents
                           Get_ATE(d, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc1")

all_results <- add_row(all_results, study_results)

## Middle Third of Income ##
d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc2 <- filter(d, INCOME3 == "Middle Income")

study_results <- bind_rows(Get_ATE(dInc2),
                           Get_ATE(dInc2, 0), # eager respondents
                           Get_ATE(dInc2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc2")

all_results <- add_row(all_results, study_results)


## Upper Third of Income ##

d <- mutate(d, INCOME3 = case_when(
      between(INCOME, 1, 8) ~ "Low Income",
      between(INCOME, 9, 12) ~ "Middle Income",
      between(INCOME, 13, 18) ~ "High Income"
))
dInc3 <- filter(d, INCOME3 == "High Income")

study_results <- bind_rows(Get_ATE(dInc3),
                           Get_ATE(dInc3, 0), # eager respondents
                           Get_ATE(dInc3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Inc3")

all_results <- add_row(all_results, study_results)

### METRO AREA ###
## Lives in a Metro Area ##

dMetro1 <- filter(d, METRO == 1)

study_results <- bind_rows(Get_ATE(dMetro1),
                           Get_ATE(dMetro1, 0), # eager respondents
                           Get_ATE(dMetro1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro1")

all_results <- add_row(all_results, study_results)

## Lives in a Non-metro Area ##
dMetro0 <- filter(d, METRO == 0)

study_results <- bind_rows(Get_ATE(dMetro0),
                           Get_ATE(dMetro0, 0), # eager respondents
                           Get_ATE(dMetro0, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Metro0")

all_results <- add_row(all_results, study_results)


### HOUSING TYPE ###

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT1 <- filter(d, HOME_TYPE3 == "Single family")

study_results <- bind_rows(Get_ATE(dHT1),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT1, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_familyhome")

all_results <- add_row(all_results, study_results)

## Lives in an apartment ##

d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT2 <- filter(d, HOME_TYPE3 == "Apartment")

study_results <- bind_rows(Get_ATE(dHT2),
                           Get_ATE(dHT1, 0), # eager respondents
                           Get_ATE(dHT2, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_apartment")

all_results <- add_row(all_results, study_results)
## Lives in a mobile home or other ##
d <- mutate(d, HOME_TYPE3 = case_when(
      between(HOME_TYPE, 1, 2) ~ "Single family",
      between(HOME_TYPE, 3, 3) ~ "Apartment",
      between(HOME_TYPE, 4, 5) ~ "Mobile home or other"))
dHT3 <- filter(d, HOME_TYPE3 == "Mobile home or other")

study_results <- bind_rows(Get_ATE(dHT3),
                           Get_ATE(dHT3, 0), # eager respondents
                           Get_ATE(dHT3, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Housing_other")

all_results <- add_row(all_results, study_results)

### RACE ###
## Whites ##
dWhite <- filter(d, RACETHNICITY == 1)
study_results <- bind_rows(Get_ATE(dWhite),
                           Get_ATE(dWhite, 0), # eager respondents
                           Get_ATE(dWhite, 1)) # reluctant respondents
study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Whites")
all_results <- add_row(all_results, study_results)

## Non-White ##

dNonwhite <- filter(d, RACETHNICITY != 1)

study_results <- bind_rows(Get_ATE(dNonwhite),
                           Get_ATE(dNonwhite, 0), # eager respondents
                           Get_ATE(dNonwhite, 1)) # reluctant respondents

study_results <- add_column(study_results, subsample = c("full", "eager", "reluctant"),
                            subgroup = "Nonwhites")
all_results <- add_row(all_results, study_results)

### PLOT DATA ###
all_results <- all_results[-1, ] #drop first row since it's all NAs

eagers <- filter(all_results, subsample == "eager")
reluctants <- filter(all_results, subsample == "reluctant")

plot_dat <- full_join(eagers, reluctants, by = "subgroup", suffix = c(".e", ".r"))

# creating significance-indicator variable for plot
plot_dat <- mutate(plot_dat, significance = case_when(
      p.value.e < .05 & p.value.r > .05 ~ "only eager",
      p.value.e < .05 & p.value.r < .05 ~ "both",
      p.value.e > .05 & p.value.r < .05 ~ "only reluctant",
      p.value.e > .05 & p.value.r > .05 ~ "neither"))

p56 <- plot_dat %>% filter(!is.na(significance)) %>%  
      ggplot(aes(estimate.e, estimate.r)) + 
      geom_abline(slope = 1, intercept = 0, color = "gray") + 
      geom_hline(yintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_vline(xintercept = 0, alpha = .5, linetype = 2, color = "gray") + 
      geom_point(size = 1.1) + 
      geom_errorbar(aes(xmin = conf.low.e, xmax = conf.high.e), 
                    alpha = .15, size = 1) + 
      geom_errorbar(aes(ymin = conf.low.r, ymax = conf.high.r), 
                    alpha = .15, size = 1) + 
      scale_color_manual(limits = c("both",  "neither", "only eager", "only reluctant"), 
                         values = c("#F8766D",  "#7CAE00", "#00BFC4", "#C77CFF")) +
      xlab("") + 
      ylab("") +
      ggtitle("56 Bai") +
      coord_cartesian(xlim =c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
      theme_classic() +
      theme(legend.position="none", 
            text = element_text(size = 14),
            aspect.ratio = 1.1,
            plot.title = element_text(hjust = 0.5, size = 10))


#### ARRANGE INTO GRID ####

fig.0 <- ggarrange(p1, p2, p3, p4, p5,
                   p6, p7, p8, p9, p11,
                   p12, p13, p14, p15, p16, 
                   p18, p19, p20, p21, p22,
                   p23, p24, p25, p27, p28, 
                   ncol = 5, nrow = 5,
                   legend = "bottom",
                   common.legend = T)
fig.0 <- annotate_figure(fig.0,
                         bottom = text_grob("Standardized Effect among Eager Respondents", color = "black"),
                         left = text_grob("Standardized Effect among Reluctant Respondents", color = "black", rot = 90))
fig.0
ggsave("../../../results/Figure_H4_pt1.pdf", width = 8.5, height = 11)

fig.1 <- ggarrange(p29, p30, p31, p32, p33, 
                   p34, p35, p36, p37, p39,
                   p40, p41, p42, p43, p44,
                   p45, p46, p47, p48, p50,
                   p51, p52, p53, p54, p56, 
                   ncol = 5, nrow = 5,
                   legend = "bottom",
                   common.legend = T)
fig.1 <- annotate_figure(fig.1,
                         bottom = text_grob("Standardized Effect among Eager Respondents", color = "black"),
                         left = text_grob("Standardized Effect among Reluctant Respondents", color = "black", rot = 90)
)
fig.1
ggsave("../../../results/Figure_H4_pt2.pdf", width = 8.5, height = 11)



